<template>
  <div>
    <div class="top">
      <el-input
        v-model="kwInput"
        placeholder="输入 任意字符 进行筛选"
        class="input-with-select"
        clearable
        @keyup.native.enter="handleSearch"
      >
        <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </el-input>
      <el-button
        type="primary"
        class="el-icon-plus add-btn"
        :disabled="listLoading"
        @click="handleAction('create', '添加配置')"
      >添加配置
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="data"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="handleSort"
    >

      <el-table-column align="center" label="网址" prop="website__domain" sortable="custom" width="300" />
      <el-table-column align="center" label="描述" prop="description" />
      <!--      <el-table-column align="center" label="token" prop="token">-->
      <!--      </el-table-column>-->
      <!--      <el-table-column align="center" label="key" prop="key">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button size="mini" type="success" @click="handlePageSize(scope.row)">{{scope.row.key}}</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column align="center" label="产品数量" prop="total_products" />
      <el-table-column align="center" label="通过产品" prop="success_products" />
      <el-table-column align="center" label="没通过" prop="failed_products" />
      <el-table-column align="center" label="审核中" prop="waiting_products" />
      <el-table-column align="center" label="过期" prop="overdue_products" />
      <el-table-column align="center" label="gtin" prop="random_gtin">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.random_gtin"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="400">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="handlePageSize(scope.row)">Feed</el-button>
          <el-button size="mini" type="success" @click="handleShowGoogleMerchant(scope.row)">GMC</el-button>
          <el-button size="mini" type="info" @click="handleShowProductModify(scope.row)">修改产品</el-button>
          <el-button size="mini" type="primary" @click="handleAction('edit', scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>
        </template>
      </el-table-column>
      `
    </el-table>

    <el-pagination
      v-if="data.length"
      background
      class="mt-pagination"
      :current-page="page"
      :page-sizes="[10,20,50,100]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
    />
    <el-button
      size="mini"
      type="success"
      @click="merchantVisible=true"
    >Google Merchent
    </el-button>
    <el-button size="mini" type="info" @click="directlyUploadTaskVisible=true"><i class="el-icon-tickets">管理上传任务</i></el-button>
    <el-button size="mini" type="warning" @click="taskVisible=true">查看爬虫队列</el-button>

    <el-dialog
      append-to-body
      :title="title"
      :width="width"
      :visible.sync="innerDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="temp" label-position="left" label-width="80px">
        <el-form-item v-if="action !== 'delete'" label="网站">
          <website_select :websites="temp.website_id" size="normal" :multiple="false" @change="function(v){temp.website_id=v}" />
          <!--          <el-select filterable v-model="temp.website_id" placeholder="选择网站">-->
          <!--            <el-option v-for="item in this.websites" :key="item.id" :label=item.domain :value=item.id></el-option>-->
          <!--          </el-select>-->
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="随机GTIN">
          <el-switch
            v-model="temp.random_gtin"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="描述">
          <el-input v-model="temp.description" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="token">
          <el-input v-model="temp.token" placeholder="prestashop专用" />
        </el-form-item>

        <el-form-item v-if="action !== 'delete'" label="key">
          <el-input v-model="temp.key" placeholder="prestashop专用" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="产品数量">
          <el-input v-model="temp.total_products" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="innerDialogVisible = false">取 消</el-button>
        <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
    <feedpagesize
      :dialog-visuable="feedPageSizeVisible"
      :feedconfig_id="feedconfig_id"
      @closeFeedPageDialog="feedPageSizeVisible=false"
    />
    <directlyuploadtask
      :task-visible="directlyUploadTaskVisible"
      @closeCrawlTaskDialog="directlyUploadTaskVisible=false"
    />
    <crawltask :task-visible="taskVisible" @closeCrawlTaskDialog="taskVisible=false" />
    <merchant :merchant-visible="merchantVisible" :website_id="current_website_id" @closeMerchantDialog="merchantVisible=false" />
    <feedproductmodify
      :pst-visible="feedproductVisual"
      :website_id="current_website_id"
      :domain="current_domain"
      :product_id="current_product_id"
      @closePstDialog="feedproductVisual=false"
    />
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/feed/feedconfig'
// import { Get as GetWebsite } from '@/api/erp/elicink/website'
import feedpagesize from '@/views/feed/elements/feedpagesize'
import qs from 'qs'
import directlyuploadtask from '@/views/crawl/elements/directlyuploadtask'
import crawltask from '@/views/crawl/elements/crawltask'
import merchant from '@/views/orders/element/merchant'
import feedproductmodify from '@/views/orders/element/feedproductmodify'
import Website_select from '../erp/elicink/elements/websites_select'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Feed',
  components: { Website_select, feedproductmodify, merchant, feedpagesize, directlyuploadtask, crawltask },
  data() {
    return {
      feedproductVisual: false,
      merchantVisible: false,
      current_website_id: '',
      current_domain: '',
      current_product_id: '',
      data: [],
      directlyUploadTaskVisible: false,
      taskVisible: false,
      listLoading: false,
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,

      action: null,
      title: '',
      temp: {
        website_id: '',
        token: '',
        random_gtin: false,
        description: '',
        total_products: 0,
        key: ''
      },
      width: '',
      websites: [],
      innerDialogVisible: false,
      feedPageSizeVisible: false,
      feedconfig_id: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleShowProductModify(row) {
      this.current_website_id = row.website_id
      this.current_product_id = ''
      this.current_domain = row.website__domain
      this.feedproductVisual = true
    },
    resetTemp() {
      this.temp = {
        website_id: '',
        token: '',
        random_gtin: false,
        description: '',
        key: ''
      }
    },
    getList() {
      // GetWebsite().then(response => {
      //   this.websites = response.data.data;
      // })
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handlePageSize(row) {
      this.feedPageSizeVisible = true
      this.feedconfig_id = row.id
    },
    handleShowGoogleMerchant(row) {
      this.current_website_id = row.website_id
      this.merchantVisible = true
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = row
          this.resetTemp()
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.title = `确认要删除${row.id}?`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '50%'
          this.title = `修改${row.id}配置`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
      }
    },
    confirm() {
      const _data = this.temp
      switch (this.action) {
        case 'create':
          if (_data.website_id === '') {
            alert('必须选择网站')
            return
          }
          if (!_data.token.length) {
            alert('必须填写token')
            return
          }
          if (!_data.key.length) {
            alert('必须填写key')
            return
          }
          Post({
            key: _data.key,
            token: _data.token,
            random_gtin: _data.random_gtin,
            description: _data.description,
            total_products: _data.total_products,
            website_id: _data.website_id

          }
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('删除成功')
          })
          break
        case
          'edit'
          :
          Put(_data.id, {
            description: _data.description,
            token: _data.token,
            key: _data.key,
            random_gtin: _data.random_gtin,
            total_products: _data.total_products,
            website_id: _data.website_id
          }).then((response) => {
            this.innerDialogVisible = false
            this.$emit('reload')
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-select .el-input {
    width: 350px;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 30px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 68px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 168px auto !important;
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 100px;
  }
</style>
